<?php
/**
 * Created by PhpStorm.
 * User: chniccs
 * Date: 2019-07-20
 * Time: 11:01
 */

namespace app\helper;


use think\facade\Env;

class CommonHelper
{
    public static function getFileExName($fileName)
    {
        return substr($fileName, strrpos($fileName, '.'));
    }
    //生成唯一id，可多机使用
    static public function create_guid($namespace = '')
    {
        static $guid = '';
        $uid = uniqid("", true);
        $data = $namespace;
        $data .= $_SERVER['REQUEST_TIME'];
        $data .= $_SERVER['HTTP_USER_AGENT'];
        $data .= $_SERVER['SERVER_ADDR'];
        $data .= $_SERVER['SERVER_PORT'];
        $data .= $_SERVER['REMOTE_ADDR'];
        $data .= $_SERVER['REMOTE_PORT'];
        $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
        $guid =
            substr($hash, 0, 8) .
            '-' .
            substr($hash, 8, 4) .
            '-' .
            substr($hash, 12, 4) .
            '-' .
            substr($hash, 16, 4) .
            '-' .
            substr($hash, 20, 12) ;
        return $guid;
    }

    public static function http_curl($url)
    {
        $curl = curl_init();//初始化
        curl_setopt($curl, CURLOPT_URL, $url);//设置抓取的url
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);// https请求 不验证证书和hosts
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_HEADER, 0);//  设置头文件的信息作为数据流输出  设置为1 的时候，会把HTTP信息打印出来  不要http header 加快效率
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//设置获取的信息以文件流的形式返回，而不是直接输出。 如果设置是0，打印信息就是true
        $data = curl_exec($curl);//执行命令
        $result = json_decode($data, true);
        if ($data == false) {
            echo "Curl Error:" . curl_error($curl);
            exit();
        }
        curl_close($curl);//关闭URL请求
        return $result;
    }

    /**
     * 上传文件到本地
     * @param null $path 要上传的路径，可选
     * @param bool $private 是否不对外部访问
     * @return array
     */
    public static function upload($path=null,$private=false) {
        if($path==null){
            $path = '/upload/' . date('Ymd', time()) . '/';
        }
        $name = $_FILES['file']['name'];
        $tmp_name = $_FILES['file']['tmp_name'];
        $error = $_FILES['file']['error'];
        //过滤错误
        if ($error) {
            switch ($error) {
                case 1 :
                    $error_message = '您上传的文件超过了PHP.INI配置文件中UPLOAD_MAX-FILESIZE的大小';
                    break;
                case 2 :
                    $error_message = '您上传的文件超过了PHP.INI配置文件中的post_max_size的大小';
                    break;
                case 3 :
                    $error_message = '文件只被部分上传';
                    break;
                case 4 :
                    $error_message = '文件不能为空';
                    break;
                default :
                    $error_message = '未知错误';
            }
            die($error_message);
        }
        $arr_name = explode('.', $name);
        $hz = array_pop($arr_name);
        $new_name = md5(time() . uniqid()) . '.' . $hz;
        $prefix= $_SERVER['DOCUMENT_ROOT'];
        if($private){
            $prefix = Env::get('root_path');
        }
        if (!file_exists($prefix . $path)) {
            mkdir($prefix. $path, 0755, true);
        }
        if (move_uploaded_file($tmp_name, $prefix. $path . $new_name)) {
            return [
                'done'=>true,
                'fileName' => $new_name,
                'filePath'  => $path . $new_name
            ];
        } else {
            return ['done'=>true,'msg'=> '文件上传失败'];
        }
    }
}
